<?php

class Helper extends CController {

    public static function truncate_utf8_string($string, $length, $etc = '...') {
        $result = '';
        $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
        $strlen = strlen($string);
        for ($i = 0; (($i < $strlen) && ($length > 0)); $i++) {
            if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
                if ($length < 1.0) {
                    break;
                }
                $result .= substr($string, $i, $number);
                $length -= 1.0;
                $i += $number - 1;
            } else {
                $result .= substr($string, $i, 1);
                $length -= 0.5;
            }
        }
        $result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
        if ($i < $strlen) {
            $result .= $etc;
        }
        return $result;
    }

    public static function getFeildByLang() {
        switch (Yii::app()->language) {
            case 'zh_cn':
                $postfix = '_cn';
                break;
            case 'zh_tw':
                $postfix = '_tw';
                break;
            case 'kr':
                $postfix = '_kr';
                break;
            default:
                $postfix = '';
        }
        return $postfix;
    }

    public static function getArrayByLang($array) {
        $newArr = array();
        foreach ($array as $key => $item) {
            $newArr[$key] = Yii::t('Lang', $item);
        }
        return $newArr;
    }
    
    public static function getTimeFByLang($time){
        switch (Yii::app()->language) {
            case 'zh_cn':
                $weekarray=array("日","一","二","三","四","五","六");
                $timeF = '星期'.$weekarray[date("w",$time)].'，'.date('Y年m月d日',$time);
                break;
            case 'zh_tw':
                $weekarray=array("日","一","二","三","四","五","六");
                $timeF = '星期'.$weekarray[date("w",$time)].'，'.date('Y年m月d日',$time);
                break;
            case 'kr':
                $timeF = date('l, F j, Y',$time);
                break;
            default:
                $timeF = date('l, F j, Y',$time);
        }
        return $timeF;
    }

}

?>